#!/usr/bin/env python

__author__='Chase Schultz'
__version__ = '0.1'

import os
import sys
from subprocess import Popen,PIPE



class Nmap():
    '''A class wrapper for nmap scans'''
    port = '1-65353' # Default port numbers
    portflag = '-p'
    scanFlags = 'sS'
    flags = [portflag, port, scanFlags,'127.0.0.1'] #Kept in for testing class
    target = '127.0.0.1'
    executablePath = '/usr/bin/nmap'
    confFilename = '.nmap'
    saveFile = 'output.scan'
    fileSaveboolean = False
    results = ''
    scanTypes = {}

    def retriveScantypes(self):
        '''Get scan types from file and return dictionary'''
        if os.path.exists(self.confFilename):
            scannerConf = open(self.confFilename, 'r')
        else:
            print 'File Does Not Exist'
            sys.exit()
        flagsList = scannerConf.readlines()
        scannerConf.close()
        clean = self.__removeNewlines__(flagsList)
        for elements in clean:
            keyAndValue = elements.split('=')
            self.scanTypes[keyAndValue[0]] = keyAndValue[1]
        
    def __removeNewlines__(self, originList):
        '''Obvious function is obvious'''
        cleanList = []
        for string in originList:
            if string[0] == '#':
                    continue
            cleanList.append(string.rstrip())
        return cleanList
    
    def saveTofile(self):
        '''Saves Results to File'''
        outputFile = open(self.saveFile, 'a')
        for fileOutputline in self.results:
                if fileOutputline != None:
                    print fileOutputline
                    outputFile.write(fileOutputline)
        outputFile.close()
            
    def displayResults(self):
        '''displays results to stdout'''
        for line in self.results:
                if line != None:
                    print line 
                      
    def runScan(self):
        '''execute scan'''
        flags = [self.portflag, self.port, self.scanFlags,self.target]
        flags.insert(0, os.path.abspath(self.executablePath)) # Insert path to binary file    
        self.results = Popen(flags, stdout=PIPE).communicate()

class Xprobe():
    '''A class wrapper for xprobe2 scans'''
    port = '1-65353' # Default port numbers
    portflag = '-T'
    scanFlags = ''
    flags = [scanFlags, port, '127.0.0.1'] #Kept in for testing class
    target = '127.0.0.1'
    executablePath = '/usr/bin/xprobe2'
    confFilename = '.xprobe2'
    saveFile = 'output.scan'
    fileSaveboolean = False
    results = ''
    scanTypes = {}

    def retriveScantypes(self):
        '''Get scan types from file and return dictionary'''
        if os.path.exists(self.confFilename):
            scannerConf = open(self.confFilename, 'r')
        else:
            print 'File Does Not Exist'
            sys.exit()
        flagsList = scannerConf.readlines()
        scannerConf.close()
        clean = self.__removeNewlines__(flagsList)
        for elements in clean:
            keyAndValue = elements.split('=')
            self.scanTypes[keyAndValue[0]] = keyAndValue[1]
        
    def __removeNewlines__(self, originList):
        '''Obvious function is obvious'''
        cleanList = []
        for string in originList:
            if string[0] == '#':
                    continue
            cleanList.append(string.rstrip())
        return cleanList
    
    def saveTofile(self):
        '''Saves Results to File'''
        outputFile = open(self.saveFile, 'a')
        for fileOutputline in self.results:
                if fileOutputline != None:
                    print fileOutputline
                    outputFile.write(fileOutputline)
        outputFile.close()
            
    def displayResults(self):
        '''displays results to stdout'''
        for line in self.results:
                if line != None:
                    print line 
                      
    def runScan(self):
        '''execute scan'''
        if '-A' in self.scanFlags:
            flags = ['-T', self.port, '-A', self.target]
        elif '-U' in self.scanFlags:
            flags = ['-U', '1-50', self.target]
        else:    
            flags = [ self.scanFlags, self.port, self.target]
        flags.insert(0, os.path.abspath(self.executablePath)) # Insert path to binary file    
        self.results = Popen(flags, stdout=PIPE).communicate()

class Netcat():
    '''A class wrapper for nmap scans'''
    port = '1-65353' # Default port numbers
    portflag = '-z'
    scanFlags = '-v'
    flags = [portflag, scanFlags,'127.0.0.1', port] #Kept in for testing class
    target = '127.0.0.1'
    executablePath = '/bin/netcat'
    confFilename = '.netcat'
    saveFile = 'output.scan'
    fileSaveboolean = False
    results = ''
    scanTypes = {}

    def retriveScantypes(self):
        '''Get scan types from file and return dictionary'''
        if os.path.exists(self.confFilename):
            scannerConf = open(self.confFilename, 'r')
        else:
            print 'File Does Not Exist'
            sys.exit()
        flagsList = scannerConf.readlines()
        scannerConf.close()
        clean = self.__removeNewlines__(flagsList)
        for elements in clean:
            keyAndValue = elements.split('=')
            self.scanTypes[keyAndValue[0]] = keyAndValue[1]
        
    def __removeNewlines__(self, originList):
        '''Obvious function is obvious'''
        cleanList = []
        for string in originList:
            if string[0] == '#':
                    continue
            cleanList.append(string.rstrip())
        return cleanList
    
    def saveTofile(self):
        '''Saves Results to File'''
        outputFile = open(self.saveFile, 'a')
        for fileOutputline in self.results:
                if fileOutputline != None:
                    print fileOutputline
                    outputFile.write(fileOutputline)
        outputFile.close()
            
    def displayResults(self):
        '''displays results to stdout'''
        for line in self.results:
                if line != None:
                    print line 
                      
    def runScan(self):
        '''execute scan'''
        flags = [self.portflag, self.scanFlags,self.target, self.port]
        flags.insert(0, os.path.abspath(self.executablePath)) # Insert path to binary file    
        self.results = Popen(flags, stdout=PIPE).communicate()

if __name__ == '__main__':
    print 'scanning...'
    scan = Nmap()
    scan.port = '1-65353'
    scan.retriveScantypes()
    print scan.scanTypes.keys()
    scan.runScan()
    scan.displayResults()